//
//  StyleListView.swift
//  MyCloset
//
//  Created by 牟好好 on 2024/11/25.
//

import SwiftUI

struct StyleListView: View {
    @Binding var selectedTab: String
    @Binding var isScroll: Bool
    @EnvironmentObject var vm: ClothStylesViewModel
    @ObservedObject var coreVm = CoreDataViewModel.shared
    @StateObject var fileVm = FileManagerViewModel()
    @State private var scHeight: CGFloat = 0
    @Namespace var topID
    @Namespace var bottomID
    
    var body: some View {
        ScrollViewReader { proxy in
            ScrollView(.vertical, showsIndicators: false) {
                VStack {
                    Spacer(minLength: 10)
                        .id(topID)
                    
                    ForEach(vm.addedStyleList[clothTypes.firstIndex(of: selectedTab) ?? 0], id: \.self) { style in
                        HStack {
                            Text(style)
                            Spacer()
                            Button(action: {
                                vm.removeStyle(from: selectedTab, styleToRemove: style) {
                                    // 删除样式时同时将包含该样式的服装删除
                                    let matchingEntities = coreVm.savedEntities.enumerated().filter { $0.element.features?.contains(style) == true }
                                    for (_, entity) in matchingEntities {
                                        if let id = entity.id, let index = coreVm.savedEntities.firstIndex(where: { $0.id == id }) {
                                            coreVm.deleteClothes(indexSet: IndexSet(integer: index))
                                            fileVm.deleteImage(id: id)
                                        }
                                    }
                                    // 将搭配列表中包含该style的项也删除
                                    let matchingCombinations = vm.recommendedCombinations.filter { combination in
                                        combination.contains(style)
                                    }
                                    for combination in matchingCombinations {
                                        vm.removeCombination(combination)
                                    }
                                }
                            }, label: {
                                Image(systemName: "trash")
                                    .foregroundStyle(.red)
                            })
                        }
                        .padding(.vertical, 5)
                    }
                    Spacer(minLength: 10)
                        .id(bottomID)
                }
                .padding(.horizontal)
                // 高度自适应
                .background(
                    GeometryReader { geo -> Color in
                        DispatchQueue.main.async {
                            scHeight = geo.size.height
                        }
                        return Color.clear
                    }
                )
            }
            .frame(maxWidth: boundsWidth * 0.8, maxHeight: scHeight)
            .overlay {
                if vm.addedStyleList[clothTypes.firstIndex(of: selectedTab) ?? 0].count > 0 {
                    RoundedRectangle(cornerRadius: 12, style: .continuous)
                        .stroke(lineWidth: 2)
                        .foregroundStyle(.blue)
                }
            }
            .onChange(of: isScroll) { _, _ in
                proxy.scrollTo(bottomID)
            }
            .onChange(of: selectedTab) { _, _ in
                proxy.scrollTo(topID)
            }
        }
    }
}

#Preview {
    StyleListView(selectedTab: .constant("上装"), isScroll: .constant(false))
        .environmentObject(ClothStylesViewModel())
}
